package cn.medemede.beans;

import cn.medemede.dao.DatabaseConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.sql.*;

public class Do_join implements Action{

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        String farward="join_failed";
        String Register_name=request.getParameter("username");
        System.out.println(Register_name);
        String Register_password=request.getParameter("password");
        System.out.println(Register_password);
        String zname=request.getParameter("zname");
        System.out.println(zname);
        String sex=request.getParameter("sex");
        System.out.println(sex);
        String borth=request.getParameter("borth");
        String phone=request.getParameter("phone");
        String zposition=request.getParameter("province")+" "+request.getParameter("city");
        String email=request.getParameter("email");
        String[] favorite=request.getParameterValues("favorite");
        String favorite_s=" ";
        if(favorite!=null) {
            for (String s : favorite) {
                favorite_s += s + " ";
            }
        }
        System.out.println(favorite_s);

        String InsertSQL="INSERT INTO users (username,password,zname,sex,borth,phone,zposition,email,favorite) VALUES (?,?,?,?,?,?,?,?,?)";
        Connection connection= DatabaseConnection.getConnection();
        PreparedStatement ps=connection.prepareStatement(InsertSQL);
        ps.setString(1,Register_name);
        ps.setString(2,Register_password);
        ps.setString(3,zname);
        ps.setString(4,sex);
        ps.setString(5,borth);
        ps.setString(6,phone);
        ps.setString(7,zposition);
        ps.setString(8,email);
        ps.setString(9,favorite_s);

        int FLAG = ps.executeUpdate();
        if (FLAG > 0) {
            System.out.println("注册成功！");
            out.println("-----注册成功！-----");
            farward="join_suc";
        } else {
            System.out.println("注册失败！");
            out.println("-----注册失败！-----");
            farward="join_failed";
        }
        connection.close();
        return farward;
    }
}
